#include "STC15Wxx.h" //自定义头文件,<>系统头文件

void System_Init(void);

sbit INT1 = 0xB3;

#define LED_WORK_STA P30   //宏定义  名称   内部存储  // sbit P31=P3^1; sbit P30 =P3^0;
#define YELLOW_BAT P31
#define BAT_LOW P33

#define STA_DOOR_OPEN P35
#define STA_DOOR_CLOSE P34

unsigned char flag_Door;//=0无动作  1关门  2开门
unsigned char DoorSta;//=0无动作  1关门  2开门
unsigned char flag_VlotLow;//=0 电池未低压    =1 电池低压   =2 电池低压中

void Delay10ms()		//@5.5296MHz  延时10ms
{
	unsigned char i, j;

	i = 54;
	j = 199;
	do
	{
		while (--j);
	} while (--i);
}

void Delay100ms()		//@5.5296MHz  延迟100ms
{
	unsigned char i, j, k;
	i = 3;
	j = 26;
	k = 223;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}


void main()
{
    System_Init();
    while(1)
    {
        if(flag_Door==1)
            {
                //门关闭
                flag_Door=0;
                Delay10ms();
                if(STA_DOOR_CLOSE==0)
                {
                    //门关闭了
                    DoorSta=1;
                }
            }
        else if(flag_Door==2)
            {
                //门打开
                flag_Door=0;
                Delay10ms();
                if(STA_DOOR_OPEN==0)
                {
                    //门打开了
                    DoorSta=2;
                }
            }
        else
            {
                flag_Door=0;
            }
        if(flag_VlotLow)
        {
            if(flag_VlotLow==1)
            {
                Delay10ms();
                if(BAT_LOW ==0)
                {
                    flag_VlotLow=2;
                }
                else
                {
                    flag_VlotLow=0;
                }

            }
        }

        if(DoorSta)
        {
            LED_WORK_STA=1;//工作状态灯打开
            Delay100ms();
            LED_WORK_STA=0;//工作状态灯关闭
            DoorSta=0;
        }
        if(flag_VlotLow==2)
        {
            //电池低压提示
            YELLOW_BAT=1;//工作状态灯打开
            Delay100ms();
            YELLOW_BAT=0;//工作状态灯关闭
        }
    }
}

void System_Init()
{
    P0M0 = 0x00;
    P0M1 = 0x00;
    P1M0 = 0x00;
    P1M1 = 0x00;
    P2M0 = 0x00;
    P2M1 = 0x00;
    P3M0 = 0x00;
    P3M1 = 0x00;
    P4M0 = 0x00;
    P4M1 = 0x00;
    P5M0 = 0x00;
    P5M1 = 0x00;
    P6M0 = 0x00;
    P6M1 = 0x00;
    P7M0 = 0x00;
    P7M1 = 0x00;

    //LED灯IO  IO口推挽输出，强上拉输出
    P3M1 &= (~0x03);  
    P3M0 |= 0x03;

    //p3.4 p3.5 开漏
    P3M0 |=0x30;
    P3M1 |=0x30;
    //LED灯关闭   初始化
    LED_WORK_STA=0;
    YELLOW_BAT=0;

    //INT1 初始化
    INT1 = 1;
    IT1 = 1;                    //设置INT1的中断类型 (1:仅下降沿 0:上升沿和下降沿)
    EX1 = 1;                    //使能INT1中断
    EA = 1;


    //INT2 中断IO
    INT_CLKO |= 0x10;               //(EX2 = 1)使能INT2中断
    //INT3 中断IO
    INT_CLKO |= 0x20;               //(EX3 = 1)使能INT3中断
    EA = 1;

    flag_Door=0;//门状态初始化
    DoorSta=0;
    flag_VlotLow=0;

    
}

//中断服务程序
void exint1() interrupt 2       //INT1中断入口
{
    flag_VlotLow=1;
    //P10 = !P10;                 //将测试口取反
}


//中断服务程序   开门检测
void exint3() interrupt 11          //INT3中断入口
{
   flag_Door=2; 
}


//中断服务程序  关门检测
void exint2() interrupt 10          //INT2中断入口
{
    flag_Door=1;
}

